home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / UNZIP_MA / SOURCE / MACSTAT.C < prev    next >
Text File  |  1992-03-01  |  6KB  |  206 lines

  1. #ifdef THINK_C
  2. #define MACOS
  3. #include    <pascal.h>
  4. #endif
  5. #ifdef MPW
  6. #define MACOS
  7. #include    <Files.h>
  8. #include    <Errors.h>
  9. #define FSFCBLen    (*(short *)0x3F6)
  10. #define CtoPstr c2pstr
  11. #define PtoCstr p2cstr
  12. #endif
  13.  
  14. #ifdef MACOS
  15. #include    <string.h>
  16. #include    "macstat.h"
  17. int stat(char *path, struct stat *buf);
  18.  
  19. /* assume that the path will contain a Mac-type pathname, i.e. ':'s, etc. */
  20. int stat(path, buf)
  21. char *path;
  22. struct stat *buf;
  23. {
  24.     char    temp[256];
  25.     short   curVolume;
  26.     long    curDir;
  27.     short   fIsHFS = false;
  28.     OSErr   err;
  29.  
  30.     if (buf == (struct stat *)0L || path == (char *)0L) {
  31.         SysBeep(1);
  32.         return -1;
  33.     }
  34.     
  35.     if (path[0] == '\0' || strlen(path)>255) {
  36.         return -1;
  37.     }
  38.  
  39.     if (GetVol((StringPtr)&temp[0], &curVolume) != noErr) {
  40.         SysBeep(1);
  41.         return -1;
  42.     }
  43.     
  44.     /* get info about the specified volume */
  45.     if (FSFCBLen > 0)   /* HFS Disk? */
  46.     {
  47.         WDPBRec wdpb;
  48.         HParamBlockRec    hpbr;
  49.         Str255 st;
  50.  
  51.         wdpb.ioCompletion = 0;
  52.         wdpb.ioNamePtr = (StringPtr)temp;
  53.         err = PBHGetVol(&wdpb, 0);
  54.         if (err == noErr)
  55.         {
  56.             hpbr.volumeParam.ioCompletion = 0;
  57.             hpbr.volumeParam.ioNamePtr = st;
  58.             hpbr.volumeParam.ioVRefNum = wdpb.ioVRefNum;
  59.             hpbr.volumeParam.ioVolIndex = 0;
  60.             err = PBHGetVInfo(&hpbr, 0);
  61.  
  62.             if (err == noErr && hpbr.volumeParam.ioVFSID == 0
  63.                 && hpbr.volumeParam.ioVSigWord == 0x4244) {
  64.                     fIsHFS = true;
  65.             }
  66.         }
  67.     }
  68.  
  69.  
  70.     /* number of links, at least in System 6.0x, 0 */
  71.     buf->st_nlink = 0;
  72.     /* user id */
  73.     buf->st_uid = 0;
  74.     /* group id */
  75.     buf->st_gid = 0;
  76.  
  77.     if (fIsHFS == true)   /* HFS? */
  78.     {
  79.         CInfoPBRec  cPB;
  80.         HParamBlockRec  wPB;
  81.         
  82.         wPB.wdParam.ioCompletion = (ProcPtr)0L;
  83.         wPB.wdParam.ioNamePtr = (StringPtr)temp;
  84.         err = PBHGetVol((WDPBPtr)&wPB, false);
  85.         if (err != noErr) {
  86.             SysBeep(1);
  87.             return -1;
  88.         }
  89.  
  90.         curVolume = wPB.wdParam.ioWDVRefNum;
  91.         buf->st_dev = curDir = wPB.wdParam.ioWDDirID;
  92.         
  93.         /* get information about file */
  94.         cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
  95.         CtoPstr(path);
  96.         strncpy(temp,path, path[0]+1);
  97.         PtoCstr(path);
  98.         cPB.hFileInfo.ioNamePtr = (StringPtr)temp;
  99.         cPB.hFileInfo.ioVRefNum = 0;
  100.         cPB.hFileInfo.ioDirID = 0;
  101.         cPB.hFileInfo.ioFDirIndex = 0;
  102.         
  103.         err = PBGetCatInfo(&cPB, false); 
  104.         
  105.         if (err != noErr) {
  106.             if (err != fnfErr) {
  107.                 SysBeep(1);
  108.             }
  109.             return -1;
  110.         }
  111.         
  112.         /* Type of file: directory or regular file */
  113.         buf->st_mode = (cPB.hFileInfo.ioFlAttrib & ioDirMask) ? S_IFDIR : S_IFREG;
  114.         
  115.         /* last access time, modification time and creation time(?) */
  116.         buf->st_atime = buf->st_mtime = cPB.hFileInfo.ioFlMdDat;
  117.         buf->st_ctime = cPB.hFileInfo.ioFlCrDat;
  118.         /* inode number */
  119.         buf->st_ino = cPB.hFileInfo.ioDirID + ((long)curVolume)<<16;
  120.         /* size of file - use only the data fork */
  121.         buf->st_size = cPB.hFileInfo.ioFlLgLen;
  122.         /* size of disk block */
  123.         if (cPB.hFileInfo.ioFlClpSiz == 0) {
  124.             HParamBlockRec hPB;
  125.             
  126.             hPB.volumeParam.ioCompletion = (ProcPtr)0L;
  127.             hPB.volumeParam.ioNamePtr = (StringPtr)temp;
  128.             hPB.volumeParam.ioVRefNum = 0;
  129.             hPB.volumeParam.ioVolIndex = 0;
  130.             
  131.             err = PBHGetVInfo(&hPB, false);
  132.             
  133.             if (err != noErr) {
  134.                 SysBeep(1);
  135.                 return -1;
  136.             }
  137.             
  138.             buf->st_blksize = hPB.volumeParam.ioVClpSiz;
  139.         }
  140.         else
  141.             buf->st_blksize = cPB.hFileInfo.ioFlClpSiz;
  142.     
  143.         buf->st_blocks = cPB.hFileInfo.ioFlPyLen/ buf->st_blksize;
  144.     }
  145.     else    /* MFS? */
  146.     {
  147.         ParamBlockRec   pPB;
  148.         
  149.         buf->st_dev = 0;
  150.         
  151.         CtoPstr(path);
  152.         strncpy(temp, path, path[0]+1);
  153.         PtoCstr(path);
  154.         pPB.fileParam.ioCompletion = (ProcPtr)0;
  155.         pPB.fileParam.ioNamePtr = (StringPtr)temp;
  156.         pPB.fileParam.ioVRefNum = curVolume;
  157.         pPB.fileParam.ioFVersNum = 0;
  158.         pPB.fileParam.ioFDirIndex = 0;
  159.         
  160.         err = PBGetFInfo(&pPB, false);   
  161.         
  162.         if (err != noErr) {
  163.             SysBeep(1);
  164.             return -1;
  165.         }
  166.         
  167.         /* Type of file: either directory or regular file */
  168.         buf->st_mode = (pPB.fileParam.ioFlAttrib & ioDirMask) ? S_IFDIR : S_IFREG;
  169.         
  170.         /* last access time, modification time and creation time(?) */
  171.         buf->st_atime = buf->st_mtime = pPB.fileParam.ioFlMdDat;
  172.         buf->st_ctime = pPB.fileParam.ioFlCrDat;
  173.         /* inode number */
  174.         buf->st_ino = pPB.fileParam.ioFlNum + ((long)curVolume)<<16;
  175.         /* size of file - use only the data fork */
  176.         buf->st_size = pPB.fileParam.ioFlLgLen;
  177.         /* size of disk block */
  178.         {
  179.             ParamBlockRec hPB;
  180.             
  181.             hPB.volumeParam.ioCompletion = (ProcPtr)0;
  182.             hPB.volumeParam.ioNamePtr = (StringPtr)temp;
  183.             hPB.volumeParam.ioVRefNum = curVolume;
  184.             hPB.volumeParam.ioVolIndex = 0;
  185.             
  186.             err = PBGetVInfo(&hPB, false);
  187.             
  188.             if (err != noErr) {
  189.                 SysBeep(1);
  190.                 return -1;
  191.             }
  192.             
  193.             buf->st_blksize = hPB.volumeParam.ioVClpSiz;
  194.         }
  195.     
  196.         /* number of disk blocks used by file - includes resource fork */
  197.         buf->st_blocks = pPB.fileParam.ioFlPyLen/ buf->st_blksize +
  198.                             pPB.fileParam.ioFlRPyLen/buf->st_blksize;
  199.     }
  200.  
  201.     return 0;
  202. }
  203. #else
  204. #error 1
  205. #endif
  206.